<template>
    <div :id="idName"></div>
</template>

<script>
import { Chart } from '@antv/g2'
export default {
    props: {
        idName: {
            type: String,
            default: 'c51',
        },
        chartData: {
            type: Array,
            default: () => [],
        },
        width: {
            type: String,
            default: '100%',
        },
        height: {
            type: Number,
            default: 300,
        },
        axis: {
            type: [Boolean, Object],
            default: true,
        },
        label: {
            type: [String, Boolean],
            default: false,
        },
    },
    watch: {
        chartData(val) {
            this.chart.changeData(val)
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.render()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                autoFit: true,
            })
            // Step 2: 载入数据源
            this.chart.data(this.chartData)

            this.chart
                .coordinate()
                .transpose()
                .scale(1, -1);
            this.chart.scale('value', {
                alias: '美金（￥）',
            });
            this.chart.axis('key', {
                label: {
                offset: 12,
                },
                tickLine: null,
                line: null,
            });
            this.chart.tooltip({
                shared: true,
                showMarkers: false,
            });
            this.chart
                .interval()
                .position('key*value')
                .color('type')
                // .size(13)
                // .label('count', (val, t) => {
                //     console.log('val',val);
                //     const color = t === '普通订单' ? 'white' : '#47494b';
                //     if (val < 1) {
                //     return null;
                //     }
                //     return {
                //     position: 'middle',
                //     offset: 0,
                //     style: {
                //         fontSize: 12,
                //         fill: color,
                //         lineWidth: 0,
                //         stroke: null,
                //         shadowBlur: 2,
                //         shadowColor: 'rgba(0, 0, 0, .45)',
                //     },
                //     };
                // })
                .adjust([
                    {
                        type: 'dodge',
                        marginRatio: 0,
                    },
                ]);
            this.chart.interaction('active-region');
            this.chart.render();
        },
    },
}
</script>
